﻿using System;
using System.Data;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing.Drawing2D;
using System.Security.Cryptography;
using System.Text;
using System.Collections.Generic;
using System.Runtime.InteropServices;

namespace KP_CommonLibrary.KP_CommonControls
{
    #region KPMessageBoxIcon
    public enum KPMessageBoxIcon
    {
        Information,
        Question,
        Warning,
        RecycleBinEmpty,
        RecycleBinFull,
        Stop,
        Error,
        FindUser,
        Help,
        Print,
        Lock
    }
    #endregion

    #region KPMessageBoxButton
    public enum KPMessageBoxButton
    {
        AbortRetryIgnore,
        OK,
        OKCancel,
        RetryCancel,
        YesNo,
        YesNoCancel
    }
    #endregion

    public partial class KPMessageBox : KP_FormBase01
    {
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern bool MessageBeep(uint type);

        static private KPMessageBox nKPMessageBox;

        static private FlowLayoutPanel flpSpace;
        static private FlowLayoutPanel flpMessage;
        static private FlowLayoutPanel flpButton;
        static private TableLayoutPanel tlpMessage;

        static private PictureBox pIcon;
        static private KP_Label lblMessage;

        static private KPButton BtnOK;
        static private KPButton BtnCancel;
        static private KPButton BtnYes;
        static private KPButton BtnNo;
        static private KPButton BtnRetry;
        static private KPButton BtnIgnore;
        static private KPButton BtnAbort;

        static private DialogResult DlgKPMessageBoxResult;

        #region CreateMessageBox
        private static void CreateMessageBox(string Title)
        {
            try
            {
                nKPMessageBox = new KPMessageBox();
                nKPMessageBox.Text = Title;
                nKPMessageBox.Size = new Size(320, 160);
                nKPMessageBox.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(227)))), ((int)(((byte)(239)))), ((int)(((byte)(255)))));
                nKPMessageBox.Font = new Font("Arial", 9, FontStyle.Regular);
                nKPMessageBox.ControlBox = false;
                nKPMessageBox.FormBorderStyle = FormBorderStyle.FixedSingle;
                nKPMessageBox.ShowIcon = false;
                nKPMessageBox.MaximizeBox = false;
                nKPMessageBox.MinimizeBox = false;

                nKPMessageBox.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
                nKPMessageBox.ShowInTaskbar = false;
                nKPMessageBox.Refresh();

                TableLayoutPanel tlp = new TableLayoutPanel();
                tlp.RowCount = 3;
                tlp.ColumnCount = 1;
                tlp.Dock = System.Windows.Forms.DockStyle.Fill;
                tlp.BackColor = System.Drawing.Color.Transparent;
                tlp.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 15));
                tlp.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 65));
                tlp.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
                tlp.Padding = new Padding(0, 0, 0, 0);
                tlp.Dock = DockStyle.Fill;
                flpSpace = new FlowLayoutPanel();
                flpSpace.Dock = System.Windows.Forms.DockStyle.Fill;
                flpSpace.BackColor = System.Drawing.Color.Transparent;

                flpMessage = new FlowLayoutPanel();
                flpMessage.BackColor = System.Drawing.Color.Transparent;
                flpMessage.Dock = System.Windows.Forms.DockStyle.Fill;

                pIcon = new PictureBox();

                lblMessage = new KP_Label();
                lblMessage.Text = "Message";

                tlpMessage = new TableLayoutPanel();
                tlpMessage.RowCount = 1;
                tlpMessage.ColumnCount = 3;
                tlpMessage.BackColor = System.Drawing.Color.Transparent;
                tlpMessage.Dock = System.Windows.Forms.DockStyle.Fill;

                tlpMessage.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 10));
                tlpMessage.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 45));
                tlpMessage.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));

                tlpMessage.Controls.Add(pIcon, 1, 0);
                tlpMessage.Controls.Add(lblMessage, 2, 0);

                flpButton = new FlowLayoutPanel();
                flpButton.BackColor = System.Drawing.Color.Transparent;
                flpButton.Dock = System.Windows.Forms.DockStyle.Fill;
                flpButton.BorderStyle = System.Windows.Forms.BorderStyle.None;
                flpButton.FlowDirection = System.Windows.Forms.FlowDirection.RightToLeft;

                tlp.Controls.Add(flpSpace, 0, 0);
                tlp.Controls.Add(tlpMessage, 0, 1);
                tlp.Controls.Add(flpButton, 0, 2);

                nKPMessageBox.Controls.Add(tlp);
                nKPMessageBox.PerformLayout();


            }
            catch { }
        }
        #endregion

        #region ProcessIcon
        private static void ProcessIcon(KPMessageBoxIcon VNMBI)
        {
            switch (VNMBI)
            {
                case KPMessageBoxIcon.Error:
                    MessageBeep(20);
                    pIcon.Image = Properties.Resources.Error;
                    break;
                case KPMessageBoxIcon.FindUser:
                    MessageBeep(10);
                    pIcon.Image = Properties.Resources.FindUser;
                    break;
                case KPMessageBoxIcon.Help:
                    MessageBeep(50);
                    pIcon.Image = Properties.Resources.Help;
                    break;
                case KPMessageBoxIcon.Information:
                    MessageBeep(50);
                    pIcon.Image = Properties.Resources.Infomation;
                    break;
                case KPMessageBoxIcon.Lock:
                    MessageBeep(10);
                    pIcon.Image = Properties.Resources.Lock;
                    break;
                case KPMessageBoxIcon.Print:
                    MessageBeep(10);
                    pIcon.Image = Properties.Resources.Print;
                    break;
                case KPMessageBoxIcon.Question:
                    MessageBeep(10);
                    pIcon.Image = Properties.Resources.Question;
                    break;
                case KPMessageBoxIcon.RecycleBinEmpty:
                    MessageBeep(50);
                    pIcon.Image = Properties.Resources.RecycleBinEmpty;
                    break;
                case KPMessageBoxIcon.RecycleBinFull:
                    MessageBeep(50);
                    pIcon.Image = Properties.Resources.RecycleBinFull;
                    break;
                case KPMessageBoxIcon.Stop:
                    MessageBeep(10);
                    pIcon.Image = Properties.Resources.Stop;
                    break;
                case KPMessageBoxIcon.Warning:
                    MessageBeep(50);
                    pIcon.Image = Properties.Resources.Warning;
                    break;
            }
        }
        #endregion

        #region CreateCmdButton
        private static void ShowOKButton()
        {
            BtnOK = new KPButton();
            BtnOK.Anchor = AnchorStyles.Right & AnchorStyles.Bottom;
            BtnOK.Text = "&Chấp nhận";
            BtnOK.Click += new EventHandler(BtnOK_Click);
            flpButton.Controls.Add(BtnOK);
        }

        private static void ShowCancelButton()
        {
            BtnCancel = new KPButton();
            BtnCancel.Text = "&Hủy bỏ";
            BtnCancel.Click += new EventHandler(BtnCancel_Click);
            flpButton.Controls.Add(BtnCancel);
        }

        private static void ShowNoButton()
        {
            BtnNo = new KPButton();
            BtnNo.Text = "&Không";
            BtnNo.Click += new EventHandler(BtnNo_Click);
            flpButton.Controls.Add(BtnNo);
        }
        private static void ShowYesButton()
        {
            BtnYes = new KPButton();
            BtnYes.Text = "&Có";
            BtnYes.Click += new EventHandler(BtnYes_Click);
            flpButton.Controls.Add(BtnYes);
        }

        private static void ShowAbortButton()
        {
            BtnAbort = new KPButton();
            BtnAbort.Text = "&Dừng lại";
            BtnAbort.Click += new EventHandler(BtnAbort_Click);
            flpButton.Controls.Add(BtnAbort);
        }

        private static void ShowIgnoreButton()
        {
            BtnIgnore = new KPButton();
            BtnIgnore.Text = "&Bỏ qua";
            BtnIgnore.Click += new EventHandler(BtnIgnore_Click);
            flpButton.Controls.Add(BtnIgnore);
        }
        private static void ShowRetryButton()
        {
            BtnRetry = new KPButton();
            BtnRetry.Text = "&Thử lại";
            BtnRetry.Click += new EventHandler(BtnRetry_Click);
            flpButton.Controls.Add(BtnRetry);
        }
        #endregion

        #region EventCmdButton_Click
        static void BtnOK_Click(object sender, EventArgs e)
        {
            DlgKPMessageBoxResult = DialogResult.OK;
            nKPMessageBox.Dispose();
        }

        static void BtnCancel_Click(object sender, EventArgs e)
        {
            DlgKPMessageBoxResult = DialogResult.Cancel;
            nKPMessageBox.Dispose();
        }
        static void BtnYes_Click(object sender, EventArgs e)
        {
            DlgKPMessageBoxResult = DialogResult.Yes;
            nKPMessageBox.Dispose();
        }

        static void BtnNo_Click(object sender, EventArgs e)
        {
            DlgKPMessageBoxResult = DialogResult.No;
            nKPMessageBox.Dispose();
        }

        static void BtnIgnore_Click(object sender, EventArgs e)
        {
            DlgKPMessageBoxResult = DialogResult.Ignore;
            nKPMessageBox.Dispose();
        }

        static void BtnAbort_Click(object sender, EventArgs e)
        {
            DlgKPMessageBoxResult = DialogResult.Abort;
            nKPMessageBox.Dispose();
        }

        static void BtnRetry_Click(object sender, EventArgs e)
        {
            DlgKPMessageBoxResult = DialogResult.Retry;
            nKPMessageBox.Dispose();
        }
        #endregion

        #region ProcessButton
        private static void ProcessButton(KPMessageBoxButton VNMBB)
        {
            switch (VNMBB)
            {
                case KPMessageBoxButton.AbortRetryIgnore:
                    ShowIgnoreButton();
                    ShowRetryButton();
                    ShowAbortButton();
                    break;
                case KPMessageBoxButton.OK:
                    nKPMessageBox.ControlBox = true;
                    ShowOKButton();
                    break;
                case KPMessageBoxButton.OKCancel:
                    ShowCancelButton();
                    ShowOKButton();
                    break;
                case KPMessageBoxButton.RetryCancel:
                    ShowCancelButton();
                    ShowRetryButton();
                    break;
                case KPMessageBoxButton.YesNo:
                    ShowNoButton();
                    ShowYesButton();
                    break;
                case KPMessageBoxButton.YesNoCancel:
                    nKPMessageBox.ControlBox = true;
                    ShowCancelButton();
                    ShowNoButton();
                    ShowYesButton();
                    break;
            }
        }
        #endregion

        #region ShowMessageBox
        public static void Show(string Message)
        {
            CreateMessageBox(" ");
            AutoSize(Message);
            lblMessage.Text = Message;
            ShowOKButton();
            MessageBeep(60);

            nKPMessageBox.ShowDialog();
        }

        public static void Show(string Message, string Title)
        {
            CreateMessageBox(Title);
            AutoSize(Message);
            lblMessage.Text = Message;
            ShowOKButton();
            MessageBeep(60);

            nKPMessageBox.ShowDialog();
        }

        public static DialogResult Show(string Message, string Title, KPMessageBoxButton VNMBB, KPMessageBoxIcon VNMBI)
        {
            CreateMessageBox(Title);
            AutoSize(Message);
            lblMessage.Text = Message;
            ProcessButton(VNMBB);
            ProcessIcon(VNMBI);

            nKPMessageBox.ShowDialog();

            return DlgKPMessageBoxResult;
        }
        #endregion
        private static new void AutoSize(string mesg)
        {
            //nKPMessageBox.PerformLayout();
            //lblMessage.Size;
            Graphics g = lblMessage.CreateGraphics();


            SizeF size2 = g.MeasureString(mesg, lblMessage.Font);
            TableLayoutPanel tbl = nKPMessageBox.Controls[0] as TableLayoutPanel;
            int height = nKPMessageBox.Height;
            if (size2.Height > tbl.RowStyles[1].Height)
            {
                tbl.RowStyles[1].Height = (int)size2.Height + 10;
                height = (int)(tbl.RowStyles[0].Height + tbl.RowStyles[1].Height + 80);
            }
            int width = nKPMessageBox.Width;
            if (size2.Width + 56 > nKPMessageBox.Width)
                width = (int)size2.Width + 56;
            nKPMessageBox.Size = new Size(width, height);
        }
    }
}
